package com.xx.news.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xx.news.BR;
import com.xx.news.R;
import com.xx.news.adapter.JingxuanAdapter;
import com.xx.news.base.BaseFragment;
import com.xx.news.databinding.ActivityJingxuanBinding;
import com.xx.news.entity.Jingxuan;
import com.xx.news.viewmodel.JingxuanViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * mvvm
 */
public class JingxuanActivity extends BaseFragment<ActivityJingxuanBinding, JingxuanViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.activity_jingxuan;//初始化布局
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }//初始化id


    private RecyclerView mRecyclerView;
    private static JingxuanAdapter mAdapter;
    private static List<Jingxuan.T1467284926140Bean> mList;

    @Override
    public void initData() {
        super.initData();
        viewModel.getDate();

        mRecyclerView = binding.recycler;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new JingxuanAdapter(mList, getActivity());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addItemClickListener(new JingxuanAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Jingxuan.T1467284926140Bean item) {
                Intent intent = new Intent(getActivity(), ToutiaoInfoActivity.class);
                intent.putExtra("bean", item);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.getLiveData().observe(this, new Observer<Jingxuan>() {
            @Override
            public void onChanged(Jingxuan toutiao) {
                //这里更新UI
                mAdapter.updateData(toutiao.getT1467284926140());
            }
        });
    }
}